package org.aldar.bquest.logic.controller.board.transform;

import org.aldar.bquest.constants.IConstants;
import org.aldar.bquest.logic.controller.board.TouchAndMoveController;
import org.aldar.bquest.logic.controller.entity.TileBoard;
import org.aldar.bquest.util.Logger;

/**
 * User: Daria
 * Date: 4/14/13 10:39 PM
 */
public class TileBoardTransform implements IConstants {

    private static float VERTICAL_STRETCH = 0.72f;

    private static Logger LOG = new Logger(TileBoardTransform.class);

    public static float[] getScreenCoords(float tileX, float tileY) {
        float screenX = (int) (tileX * 1 + tileY * -1 + a());
        float screenY = (int) (tileX * 1 + tileY * 1 + b());
        float[] result = new float[]{screenX, CAMERA_HEIGHT - screenY * VERTICAL_STRETCH};
        float[] reverse = getTileCoords(result[0], result[1]);
        return result;
    }

    public static float[] getTileCoords(float screenX, float screenY) {
        float tileX = (screenX + (CAMERA_HEIGHT - screenY) / VERTICAL_STRETCH - a() - b()) / 2;
        float tileY = ((CAMERA_HEIGHT - screenY) / VERTICAL_STRETCH - screenX + a() - b()) / 2;
        return new float[]{tileX, tileY};
    }

    public static int a() {
        return IConstants.CAMERA_WIDTH / 2;
    }

    public static int b() {
        return (int) (IConstants.CAMERA_HEIGHT / 6.35) /*- IConstants.CELL_HEIGHT / 2*/;
    }


    public static float[] getDragDropCoords(float touchScreenX, float touchScreenY) {
        float[] origOrthoXY = new float[]{TouchAndMoveController.sourceCol * CELL_WIDTH, TouchAndMoveController.sourceRow * CELL_HEIGHT};
        float[] origScreenXY = getScreenCoords(origOrthoXY);
//        float[] origScreenXY = TouchAndMoveController.aXY;
//        float[] origOrthoXY = getTileCoords(origScreenXY[0],origScreenXY[1]);
        float[] result;
        float[] touchTileCoords = getTileCoords(touchScreenX, touchScreenY);
        int approachCol = TouchAndMoveController.sourceCol;
        int approachRow = TouchAndMoveController.sourceRow;

        float[] vector = new float[]{touchTileCoords[0] - CELL_WIDTH / 2 - origOrthoXY[0], touchTileCoords[1] - CELL_HEIGHT / 2 - origOrthoXY[1]};

        if (Math.abs(vector[0]) - Math.abs(vector[1]) > 0) {
            int flag;
            if (vector[0] > 0) {
                approachCol += 1;
                flag = 1;
            } else {
                approachCol -= 1;
                flag = -1;
            }
            float newX = touchTileCoords[0] - CELL_WIDTH / 2;
            if (Math.abs(newX - origOrthoXY[0]) < (CELL_WIDTH / 3)) {
                newX = origOrthoXY[0];
                approachCol = TouchAndMoveController.sourceCol;
                approachRow = TouchAndMoveController.sourceRow;
            } else if (Math.abs(newX - origOrthoXY[0]) > CELL_WIDTH) {
                newX = origOrthoXY[0] + flag * CELL_WIDTH;
            }
            result = getSpriteAdjustedScreenCoords(getScreenCoords(newX, origOrthoXY[1]));
        } else if (Math.abs(vector[0]) - Math.abs(vector[1]) < 0) {
            int flag;
            if (vector[1] > 0) {
                flag = 1;
                approachRow += 1;

            } else {
                flag = -1;
                approachRow -= 1;
            }
            float newY = touchTileCoords[1] - CELL_HEIGHT / 2;
            if (Math.abs(newY - origOrthoXY[1]) < (CELL_HEIGHT / 3)) {
                newY = origOrthoXY[1];
                approachCol = TouchAndMoveController.sourceCol;
                approachRow = TouchAndMoveController.sourceRow;
            } else if (Math.abs(newY - origOrthoXY[1]) > CELL_HEIGHT) {
                newY = origOrthoXY[1] + flag * CELL_HEIGHT;
            }
            result = getSpriteAdjustedScreenCoords(getScreenCoords(origOrthoXY[0], newY));
        } else {
            result = getSpriteAdjustedScreenCoords(origScreenXY[0], origScreenXY[1]);
            approachCol = TouchAndMoveController.sourceCol;
            approachRow = TouchAndMoveController.sourceRow;
        }
        if (approachCol >= CELLS_HORIZONTAL || approachCol < 0
                || approachRow >= CELLS_VERTICAL || approachRow < 0
                || !TileBoard.isCellValid(approachCol, approachRow)) {
            result = getSpriteAdjustedScreenCoords(origScreenXY[0], origScreenXY[1]);
        }
        if (TouchAndMoveController.targetCol != approachCol ||
                TouchAndMoveController.targetRow != approachRow) {
            LOG.i("Drag NEW Target col/row: " + approachCol + " " + approachRow);
            //test bug wrong direction!!!
            //FIXED 28.01.15
                    /*

            Rectangle r = new Rectangle(touchScreenX, touchScreenY, 5, 5, MainBoardScreen.getScm().getVertexBufferObjectManager());
            r.setColor(Color.GREEN);
            MainBoardScreen.getScm().getTestLayer().attachChild(r);
            Text myText = new Text(touchScreenX, touchScreenY, ResourceManager.getInstance().getBigFont(), approachCol + "_" + approachRow, 100, MainBoardScreen.getScm().getVertexBufferObjectManager());
            myText.setScale(0.5f);
            MainBoardScreen.getScm().getTestLayer().attachChild(myText);
           */

        }
        TouchAndMoveController.targetCol = approachCol;
        TouchAndMoveController.targetRow = approachRow;
        return result;
    }

    public static float[] getSpriteAdjustedScreenCoords(float[] screenCoords) {
        return getSpriteAdjustedScreenCoords(screenCoords[0], screenCoords[1]);
    }

    public static int getCol(int x) {
        return (int) (x / CELL_WIDTH);
    }

    public static int getRow(int y) {
        return (int) (y / CELL_HEIGHT);
    }

    public static int getColFromScreenXY(int scX, int scY) {
        return getCol((int) getTileCoords(scX, scY)[0]);
    }

    public static int getRowFromScreenXY(int scX, int scY) {
        return getRow((int) getTileCoords(scX, scY)[1]);
    }

    public static float[] getSpriteAdjustedScreenCoords(float screenCellX, float screenCellY) {
        float[] cellOrthoCoords = getTileCoords(screenCellX, screenCellY);
        return getScreenCoords(cellOrthoCoords[0] - (float) (CELL_WIDTH * 0.75), cellOrthoCoords[1] + CELL_HEIGHT / 4);
    }

    public static float[] getScreenCoords(float[] orthoCoords) {
        return getScreenCoords(orthoCoords[0], orthoCoords[1]);
    }
}
